﻿// 8. Write a method that adds two positive integer numbers represented as arrays of digits (each array element arr[i] 
// contains a digit; the last digit is kept in arr[0]). Each of the numbers that will be added could have up to 10 000 digits.

using System;
using System.Numerics;

class SumNumbersPresentedInArrays
{
    static void Main()
    {
        Console.Write("Enter first positive number : ");
        BigInteger number1 = BigInteger.Parse(Console.ReadLine());

        Console.Write("Enter second positive number : ");
        BigInteger number2 = BigInteger.Parse(Console.ReadLine());

        // get the number of digits of the numbers so an array could be created
        int countDigits1 = countDigitsOfNumber(number1);
        int countDigits2 = countDigitsOfNumber(number2);

        // get the legnth of the result array; it should be with 1 bigger than the bigger length of the other 2 arrays
        int resultArrayLength = (countDigits1 > countDigits2 ? countDigits1 : countDigits2) + 1;

        //create the two arrays that will handle the entered numbers
        int[] array1 = new int[countDigits1];
        int[] array2 = new int[countDigits2];

        // put the numbers in the arrays
        array1 = PresentNumberInArray(countDigits1, number1);
        array2 = PresentNumberInArray(countDigits2, number2);

        // sum the arrays
        int[] resultSum = new int[resultArrayLength];
        resultSum = SumDigitArrays(resultArrayLength, array1, array2);

        // get the result as string and display it on the console
        string resultNumber = GetResultSumAsNumber(resultSum);
        Console.WriteLine("And the sum of the two numbers {0} and {1} is {2}.", number1, number2, resultNumber);
    }

    static int countDigitsOfNumber(BigInteger number)
    {
        int countDigits = 0;
        while (number > 0)
        {
            BigInteger remainder = number % 10;
            number /= 10;
            countDigits++;
        }
        return countDigits;
    }

    static int[] PresentNumberInArray(int length, BigInteger number)
    {
        int[] array = new int[length];

        for (int index = 0; index < length; index++)
        {
            int remainder = (int)(number % 10);
            number /= 10;
            array[index] = remainder;
        }

        return array;
    }

    static int[] SumDigitArrays(int resultArrayLength, int[] array1, int[] array2)
    {
        int temp = 0; // when you sum and you get something on mind
        int[] resultArray = new int[resultArrayLength];
        for (int i = 0; i < resultArrayLength; i++)
        {
            int var1 = 0;
            int var2 = 0;
            if (i >= array1.Length)
            {
                var1 = 0;
            }
            else
            {
                var1 = array1[i];
            }

            if (i >= array2.Length)
            {
                var2 = 0;
            }
            else
            {
                var2 = array2[i];
            }

            int tempSum = var1 + var2 + temp;
            int remainder = tempSum % 10;
            resultArray[i] = remainder;
            temp = tempSum / 10;
            tempSum = 0;
        }

        return resultArray;
    }

    static string GetResultSumAsNumber(int[] resultSum)
    {
        string resultStr = "";

        for (int i = 0; i < resultSum.Length - 1; i++)
        {
            resultStr = resultSum[i] + resultStr;
        }

        // remove the zero in front if it exists
        if (resultSum[resultSum.Length - 1] != 0)
        {
            resultStr = resultSum[resultSum.Length - 1] + resultStr;
        }

        return resultStr;
    }
}
